home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zpcolor.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  7.8 KB  |  261 lines

  1. /* Copyright (C) 1994, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zpcolor.c,v 1.3 2000/09/19 19:00:55 lpd Exp $ */
  20. /* Pattern color */
  21. #include "ghost.h"
  22. #include "oper.h"
  23. #include "gscolor.h"
  24. #include "gsmatrix.h"
  25. #include "gsstruct.h"
  26. #include "gxcspace.h"
  27. #include "gxfixed.h"        /* for gxcolor2.h */
  28. #include "gxcolor2.h"
  29. #include "gxdcolor.h"        /* for gxpcolor.h */
  30. #include "gxdevice.h"
  31. #include "gxdevmem.h"        /* for gxpcolor.h */
  32. #include "gxpcolor.h"
  33. #include "estack.h"
  34. #include "ialloc.h"
  35. #include "icremap.h"
  36. #include "istruct.h"
  37. #include "idict.h"
  38. #include "idparam.h"
  39. #include "igstate.h"
  40. #include "ipcolor.h"
  41. #include "store.h"
  42.  
  43. /* Imported from gspcolor.c */
  44. extern const gs_color_space_type gs_color_space_type_Pattern;
  45.  
  46. /* Forward references */
  47. private int zPaintProc(P2(const gs_client_color *, gs_state *));
  48. private int pattern_paint_prepare(P1(i_ctx_t *));
  49. private int pattern_paint_finish(P1(i_ctx_t *));
  50.  
  51. /* GC descriptors */
  52. private_st_int_pattern();
  53.  
  54. /* Initialize the Pattern cache. */
  55. private int
  56. zpcolor_init(i_ctx_t *i_ctx_p)
  57. {
  58.     gstate_set_pattern_cache(igs,
  59.                  gx_pattern_alloc_cache(imemory_system,
  60.                            gx_pat_cache_default_tiles(),
  61.                           gx_pat_cache_default_bits()));
  62.     return 0;
  63. }
  64.  
  65. /* Create an interpreter pattern structure. */
  66. int
  67. int_pattern_alloc(int_pattern **ppdata, const ref *op, gs_memory_t *mem)
  68. {
  69.     int_pattern *pdata =
  70.     gs_alloc_struct(mem, int_pattern, &st_int_pattern, "int_pattern");
  71.  
  72.     if (pdata == 0)
  73.     return_error(e_VMerror);
  74.     pdata->dict = *op;
  75.     *ppdata = pdata;
  76.     return 0;
  77. }
  78.  
  79. /* <pattern> <matrix> .buildpattern1 <pattern> <instance> */
  80. private int
  81. zbuildpattern1(i_ctx_t *i_ctx_p)
  82. {
  83.     os_ptr op = osp;
  84.     os_ptr op1 = op - 1;
  85.     int code;
  86.     gs_matrix mat;
  87.     float BBox[4];
  88.     gs_client_pattern template;
  89.     int_pattern *pdata;
  90.     gs_client_color cc_instance;
  91.     ref *pPaintProc;
  92.  
  93.     check_type(*op1, t_dictionary);
  94.     check_dict_read(*op1);
  95.     gs_pattern1_init(&template);
  96.     if ((code = read_matrix(op, &mat)) < 0 ||
  97.     (code = dict_uid_param(op1, &template.uid, 1, imemory, i_ctx_p)) != 1 ||
  98.     (code = dict_int_param(op1, "PaintType", 1, 2, 0, &template.PaintType)) < 0 ||
  99.     (code = dict_int_param(op1, "TilingType", 1, 3, 0, &template.TilingType)) < 0 ||
  100.     (code = dict_floats_param(op1, "BBox", 4, BBox, NULL)) < 0 ||
  101.     (code = dict_float_param(op1, "XStep", 0.0, &template.XStep)) != 0 ||
  102.     (code = dict_float_param(op1, "YStep", 0.0, &template.YStep)) != 0 ||
  103.     (code = dict_find_string(op1, "PaintProc", &pPaintProc)) <= 0
  104.     )
  105.     return_error((code < 0 ? code : e_rangecheck));
  106.     check_proc(*pPaintProc);
  107.     template.BBox.p.x = BBox[0];
  108.     template.BBox.p.y = BBox[1];
  109.     template.BBox.q.x = BBox[2];
  110.     template.BBox.q.y = BBox[3];
  111.     template.PaintProc = zPaintProc;
  112.     code = int_pattern_alloc(&pdata, op1, imemory);
  113.     if (code < 0)
  114.     return code;
  115.     template.client_data = pdata;
  116.     code = gs_makepattern(&cc_instance, &template, &mat, igs, imemory);
  117.     if (code < 0) {
  118.     ifree_object(pdata, "int_pattern");
  119.     return code;
  120.     }
  121.     make_istruct(op, a_readonly, cc_instance.pattern);
  122.     return code;
  123. }
  124.  
  125. /* <array> .setpatternspace - */
  126. /* In the case of uncolored patterns, the current color space is */
  127. /* the base space for the pattern space. */
  128. private int
  129. zsetpatternspace(i_ctx_t *i_ctx_p)
  130. {
  131.     os_ptr op = osp;
  132.     gs_color_space cs;
  133.     uint edepth = ref_stack_count(&e_stack);
  134.     int code;
  135.  
  136.     check_read_type(*op, t_array);
  137.     switch (r_size(op)) {
  138.     case 1:        /* no base space */
  139.         cs.params.pattern.has_base_space = false;
  140.         break;
  141.     default:
  142.         return_error(e_rangecheck);
  143.     case 2:
  144.         cs = *gs_currentcolorspace(igs);
  145.         if (cs_num_components(&cs) < 0)    /* i.e., Pattern space */
  146.         return_error(e_rangecheck);
  147.         /* We can't count on C compilers to recognize the aliasing */
  148.         /* that would be involved in a direct assignment, so.... */
  149.         {
  150.         gs_paint_color_space cs_paint;
  151.  
  152.         cs_paint = *(gs_paint_color_space *) & cs;
  153.         cs.params.pattern.base_space = cs_paint;
  154.         }
  155.         cs.params.pattern.has_base_space = true;
  156.     }
  157.     gs_cspace_init(&cs, &gs_color_space_type_Pattern, NULL);
  158.     code = gs_setcolorspace(igs, &cs);
  159.     if (code < 0) {
  160.     ref_stack_pop_to(&e_stack, edepth);
  161.     return code;
  162.     }
  163.     pop(1);
  164.     return (ref_stack_count(&e_stack) == edepth ? 0 : o_push_estack);    /* installation will load the caches */
  165. }
  166.  
  167. /* ------ Initialization procedure ------ */
  168.  
  169. const op_def zpcolor_l2_op_defs[] =
  170. {
  171.     op_def_begin_level2(),
  172.     {"2.buildpattern1", zbuildpattern1},
  173.     {"1.setpatternspace", zsetpatternspace},
  174.         /* Internal operators */
  175.     {"0%pattern_paint_prepare", pattern_paint_prepare},
  176.     {"0%pattern_paint_finish", pattern_paint_finish},
  177.     op_def_end(zpcolor_init)
  178. };
  179.  
  180. /* ------ Internal procedures ------ */
  181.  
  182. /* Render the pattern by calling the PaintProc. */
  183. private int pattern_paint_cleanup(P1(i_ctx_t *));
  184. private int
  185. zPaintProc(const gs_client_color * pcc, gs_state * pgs)
  186. {
  187.     /* Just schedule a call on the real PaintProc. */
  188.     r_ptr(&gs_int_gstate(pgs)->remap_color_info,
  189.       int_remap_color_info_t)->proc =
  190.     pattern_paint_prepare;
  191.     return_error(e_RemapColor);
  192. }
  193. /* Prepare to run the PaintProc. */
  194. private int
  195. pattern_paint_prepare(i_ctx_t *i_ctx_p)
  196. {
  197.     gs_state *pgs = igs;
  198.     gs_pattern1_instance_t *pinst =
  199.     (gs_pattern1_instance_t *)gs_currentcolor(pgs)->pattern;
  200.     ref *pdict = &((int_pattern *) pinst->template.client_data)->dict;
  201.     gx_device_pattern_accum *pdev;
  202.     int code;
  203.     ref *ppp;
  204.  
  205.     check_estack(5);
  206.     pdev = gx_pattern_accum_alloc(imemory, "pattern_paint_prepare");
  207.     if (pdev == 0)
  208.     return_error(e_VMerror);
  209.     pdev->instance = pinst;
  210.     pdev->bitmap_memory = gstate_pattern_cache(pgs)->memory;
  211.     code = (*dev_proc(pdev, open_device)) ((gx_device *) pdev);
  212.     if (code < 0) {
  213.     ifree_object(pdev, "pattern_paint_prepare");
  214.     return code;
  215.     }
  216.     code = gs_gsave(pgs);
  217.     if (code < 0)
  218.     return code;
  219.     code = gs_setgstate(pgs, pinst->saved);
  220.     if (code < 0) {
  221.     gs_grestore(pgs);
  222.     return code;
  223.     }
  224.     gx_set_device_only(pgs, (gx_device *) pdev);
  225.     push_mark_estack(es_other, pattern_paint_cleanup);
  226.     ++esp;
  227.     make_istruct(esp, 0, pdev);
  228.     push_op_estack(pattern_paint_finish);
  229.     dict_find_string(pdict, "PaintProc", &ppp);        /* can't fail */
  230.     *++esp = *ppp;
  231.     *++esp = *pdict;        /* (push on ostack) */
  232.     return o_push_estack;
  233. }
  234. /* Save the rendered pattern. */
  235. private int
  236. pattern_paint_finish(i_ctx_t *i_ctx_p)
  237. {
  238.     gx_device_pattern_accum *pdev = r_ptr(esp, gx_device_pattern_accum);
  239.     gx_color_tile *ctile;
  240.     int code = gx_pattern_cache_add_entry((gs_imager_state *)igs,
  241.                       pdev, &ctile);
  242.  
  243.     if (code < 0)
  244.     return code;
  245.     esp -= 2;
  246.     pattern_paint_cleanup(i_ctx_p);
  247.     return o_pop_estack;
  248. }
  249. /* Clean up after rendering a pattern.  Note that iff the rendering */
  250. /* succeeded, closing the accumulator won't free the bits. */
  251. private int
  252. pattern_paint_cleanup(i_ctx_t *i_ctx_p)
  253. {
  254.     gx_device_pattern_accum *const pdev =
  255.     r_ptr(esp + 2, gx_device_pattern_accum);
  256.  
  257.     /* grestore will free the device, so close it first. */
  258.     (*dev_proc(pdev, close_device)) ((gx_device *) pdev);
  259.     return gs_grestore(igs);
  260. }
  261.